package org.shoukaiseki.initdict.handle;

import org.shoukaiseki.initdict.logger.InitDictLoggers;
import org.shoukaiseki.kakaroto.annotation.DictTypeEnum;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ClassPathEnumDefinitionScanner;
import org.springframework.core.type.filter.AnnotationTypeFilter;

import java.util.Set;

public class DictTypeEnumPackageScanHandle extends ClassPathEnumDefinitionScanner {


    public static final Logger logger = InitDictLoggers.DEFAULT_LOGGER;


    public DictTypeEnumPackageScanHandle(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
        super(registry, useDefaultFilters);
    }


    protected Set<Class> doScan(String... basePackages) {
        //添加过滤条件
        addIncludeFilter(new AnnotationTypeFilter(DictTypeEnum.class));
        //调用spring的扫描
        Set<Class> beanDefinitionHolders = super.doScan(basePackages);
//        if(beanDefinitionHolders.size() != 0){
//            for (BeanDefinitionHolder beanDefinitionHolder : beanDefinitionHolders) {
//                logger.debugLambar(()->{
////                    beanDefinitionHolder.getBeanDefinition().getc
//                    logger.debug("beanDefinitionHolder");
//                    logger.debug("beanDefinitionHolder={}", JSONObject.toJSONString(beanDefinitionHolder));
//                });
//            }
//        }
        return beanDefinitionHolders;
    }


    @Override
    protected boolean isCandidateComponent(AnnotatedBeanDefinition beanDefinition) {
        return beanDefinition.getMetadata().isInterface() && beanDefinition.getMetadata().isIndependent();
    }


}
